{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Alessandro Parisi \n",
      "last updated: 2019-03-15 \n",
      "\n",
      "CPython 3.5.4\n",
      "IPython 6.1.0\n",
      "\n",
      "numpy 1.15.2\n",
      "pandas 0.20.3\n",
      "matplotlib 2.2.2\n",
      "sklearn 0.20.0\n",
      "seaborn 0.8.0\n"
     ]
    }
   ],
   "source": [
    "%load_ext watermark\n",
    "%watermark -a \"Alessandro Parisi\" -u -d -v -p numpy,pandas,matplotlib,sklearn,seaborn\n",
    "# to install watermark launch 'pip install watermark' at command line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import pandas as pd \n",
    "import matplotlib.pyplot as plt \n",
    " \n",
    "from sklearn.cluster import KMeans \n",
    "from sklearn.metrics import silhouette_score \n",
    "\n",
    "import warnings \n",
    "warnings.simplefilter('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "malware_dataset = pd.read_csv('../datasets/MalwareArtifacts.csv', delimiter=',')\n",
    "# Extacting artifacts samples fields 'MajorLinkerVersion,MajorImageVersion,MajorOperatingSystemVersion,DllCharacteristics'\n",
    "samples = malware_dataset.iloc[:, [1,2,3,4]].values\n",
    "targets = malware_dataset.iloc[:, 8].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K-means labels: [0 0 0 ... 0 1 0]\n",
      "\n",
      "K-means Clustering Results:\n",
      "\n",
      " Predicted      0      1\n",
      "Observed               \n",
      "0          83419  13107\n",
      "1           7995  32923\n",
      "\n",
      "Silhouette coefficient: 0.975\n"
     ]
    }
   ],
   "source": [
    "k_means = KMeans(n_clusters=2,max_iter=300)\n",
    "k_means.fit(samples) \n",
    "\n",
    "print(\"K-means labels: \" + str(k_means.labels_))\n",
    "print (\"\\nK-means Clustering Results:\\n\\n\", pd.crosstab(targets, k_means.labels_,rownames = [\"Observed\"],colnames = [\"Predicted\"]) )      \n",
    "print (\"\\nSilhouette coefficient: %0.3f\" % silhouette_score(samples, k_means.labels_, metric='euclidean')) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Py35",
   "language": "python",
   "name": "py35"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
